//
//  NSMessageView.m
//  Meboo
//
//  Created by kyo on 2024/5/10.
//

#import "NSMessageView.h"
#import "NSChatListViewController.h"
@interface NSMessageView ()
@property(nonatomic,strong) UIImageView *wlmImageView;
@property(nonatomic,strong) UILabel *numLab;
@end

@implementation NSMessageView

#pragma mark - Life Cycle
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
        [MBNotificationCenter addObserver:self selector:@selector(updateMessageNum) name:UPDATE_MESSAGE_UNREAD object:nil];
    }
    return self;
}

- (void)updateMessageNum {
    self.numLab.text = [NSString stringWithFormat:@"%d",[MBUserAccount defaultAccount].unReadNum];
    if ([MBUserAccount defaultAccount].unReadNum > 99) {
        self.numLab.hidden = NO;
        self.numLab.text = @"99+";
    }
    if ([MBUserAccount defaultAccount].unReadNum <= 0) {
        self.numLab.hidden = YES;
    } else {
        self.numLab.hidden = NO;
    }
}

- (void)messageAction {
    NSArray *displayConversationTypeArray = @[@(ConversationType_PRIVATE)];
    NSChatListViewController *conversationListVC = [[NSChatListViewController alloc] initWithDisplayConversationTypes:displayConversationTypeArray
                                                                                           collectionConversationType:nil];
    [conversationListVC setConversationAvatarStyle:RC_USER_AVATAR_CYCLE];
    [self.currentViewController.navigationController pushViewController:conversationListVC animated:YES];
}

#pragma mark - UI
- (void)setupUI {
    
    UITapGestureRecognizer *actionTap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(messageAction)];
    [self addGestureRecognizer:actionTap];
    
    self.backgroundColor = UIColor.clearColor;
    
    [self addSubview:self.wlmImageView];
    [self.wlmImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.equalTo(self);
        make.top.mas_equalTo(MBHeightScale(4));
        make.right.mas_equalTo(MBHeightScale(-4));
    }];
    
    [self addSubview:self.numLab];
    [self.numLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.equalTo(self);
        make.width.height.mas_equalTo(MBHeightScale(16));
    }];
    
    [self updateMessageNum];
}

#pragma mark - Setter && Getter

- (UIImageView *)wlmImageView {
    if (!_wlmImageView) {
        _wlmImageView = [UIImageView new];
        _wlmImageView.contentMode = UIViewContentModeScaleAspectFill;
        _wlmImageView.clipsToBounds = YES;
        _wlmImageView.image = ImageWithName(@"common_chat");
    }
    return _wlmImageView;
}
- (UILabel *)numLab {
    if (!_numLab) {
        _numLab = [UILabel new];
        _numLab.text = @"0";
        _numLab.textAlignment = NSTextAlignmentCenter;
        _numLab.backgroundColor = HEXS(@"FF485F");
        _numLab.layer.cornerRadius = MBHeightScale(8);
        _numLab.layer.masksToBounds = YES;
        _numLab.font = MBHelFontSizeScale(8);
        _numLab.textColor = HEXS(@"ffffff");
        _numLab.hidden = YES;
    }
    return _numLab;
}
@end
